package com.yl.ydk.base

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import com.yl.ydk.R
import com.yl.ydk.view.LoadingDialog
import com.yl.ydk.view.SwipeBackActivity.SwipeBackActivity
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseActivity : SwipeBackActivity(){

    private var loadingDialog: LoadingDialog? = null

    @SuppressLint("SourceLockedOrientationActivity")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //取消状态栏
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )

        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        val params: WindowManager.LayoutParams = window.attributes
        params.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE
        window.attributes = params
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }

        setContentView(getLayoutId())
        attachView()
        initView()

    }


    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            window.decorView.systemUiVisibility =
                (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun evenBusMessage(baseEvenBus: BaseEvenBus) {
        when (baseEvenBus.messageId) {

//            AEvent.AEVENT_VOIP_REV_CALLING_AUDIO -> { // 有护士请求视频通话
//
//                BaseApplication.MAIN_EXECUTOR.schedule({
////                    playUrl(R.raw.hushi)
//                },1, TimeUnit.SECONDS)
//
//                val intent = Intent(this, VoipActivity::class.java)
//                intent.putExtra("data", 1)
//                intent.putExtra("targetId",baseEvenBus.targetId)
//                startActivityIn(intent, this)
//            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        detachView()
        EventBus.getDefault().unregister(this)
    }

    private fun finishActivity() {
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out)
    }

    fun startActivityIn(
        intent: Intent?,
        curAct: Activity
    ) {
        if (intent != null) {
            curAct.startActivity(intent)
            curAct.overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out)
        }
    }

    override fun finish() {
        super.finish()
        finishActivity()
    }

    open fun showLoadingDialog(text: String?) {
        dismissLoadingDialog()
        val builder: LoadingDialog.Builder =
            LoadingDialog.Builder(this)
                .setMessage(text)
                .setCancelable(true)
        loadingDialog = builder.create()
        loadingDialog?.show()
    }

    open fun dismissLoadingDialog() {
        if (null != loadingDialog && loadingDialog!!.isShowing) {
            loadingDialog?.dismiss()
            loadingDialog = null
        }
    }

    abstract fun getLayoutId(): Int

    abstract fun attachView()

    abstract fun detachView()

    abstract fun initView()


}